<template>
  <!-- 可以放一个加载动画 -->
  <div v-if="loading" class="loading-container">
    <p>Loading...</p>
    <!-- 可以在这里添加加载动画 -->
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import axios from 'axios'

const route = useRoute()
const router = useRouter()
const loading = ref(true)

const fetchUserAndRedirect = async () => {
  try {
    const res = await axios.post('http://127.0.0.1:8000/home/reaa_user/', {
      id: route.query.id
    })
    
    // 根据你的路由配置调整跳转逻辑
    switch (res.data) {
      case 1:
        router.push({ name: 'home' }) // 对应路由 name: 'HrHome'
        break
      case 2:
        router.push({ name: 'HrHome' }) // 对应路由 name: 'jobHome'
        break
      default:
        router.push({ name: 'notFound' }) // 跳转到404页面
    }
  } catch (error) {
    console.error('Error:', error)
    router.push({ name: 'error' }) // 跳转到错误页
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  fetchUserAndRedirect()
})
</script>

<style scoped>
.loading-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
</style>